package com.haitaoit.chengquan.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Administrator on 2017/5/19.
 */

public class ImageUtils {
    //拍照保存目录
    public static final String fileSavePath=Environment.getExternalStorageDirectory().getPath() + "/haitao/save/";
    //图片压缩保存目录
    public static final String filePath=Environment.getExternalStorageDirectory().getPath() + "/haitao/compression/";
    public static final String fileName= Long.toString(System.nanoTime())+".png";
    public static void makeFolder(String path) {
        makeFolder(path,false);
    }
    public static void makeFolder(String path,boolean isHidden) {
        File appDir = new File(path);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        if(isHidden){
            File hiddenDir = new File(path+".nomedia");
            if (!hiddenDir.exists()) {
                hiddenDir.mkdirs();
            }
        }

    }
    public static String saveBitmap(Bitmap bmp) {

        File appDir = new File(filePath);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        File f = new File(filePath, fileName);
        try {
            FileOutputStream out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath+fileName;
    }
    public static String saveImage(Bitmap bmp) {
//        File appDir = new File(Environment.getExternalStorageDirectory(), "/haitao/compression/");
        String imgName= Long.toString(System.nanoTime())+".png";
        File appDir = new File(filePath);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        File file = new File(appDir, imgName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath+imgName;
    }

    public static void saveFile(File file,String path,String fileName) {
        try {
            InputStream input = new FileInputStream(file);
            saveFile(input,path,fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void saveFile(InputStream inputStream,String path,String fileName) {
        File tempFile = new File(path);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        OutputStream os = null;
        try {
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
//            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            os = new FileOutputStream(path + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * @param fileName
     * @return 返回k 不是kb
     */
    public static long getFileSize(String fileName) {
        long lgth = 0;
        File file = new File(fileName);
        if (file.exists()) {
            lgth = file.length();
        }
        file = null;
        return lgth;
    }

    /**
     * 压缩大于700KB的图片
     * @param imgPath
     * @return
     */
    public static String getSmallBitmap(String imgPath,String savePath,String fileName) {
        long fileSize = MyUtils.getFileSize(imgPath);
        //图片较小,就不压缩
        if(fileSize<(700*1024)){
            return imgPath;
        }
        //图片较大,压缩图片
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imgPath, options);
        options.inSampleSize = 4;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory.decodeFile(imgPath, options);
        if(bm == null){
            return  null;
        }
        //针对三星手机图片角度旋转问题
        int degree = MyUtils.getBitmapDegree(imgPath);
        if (degree!=0){
            bm = MyUtils.toTurn(bm, degree);
        }
        ByteArrayOutputStream baos = null ;
        try{
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 70, baos);//50
        }finally{
            try {
                if(baos != null)
                    baos.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        String filePath=Environment.getExternalStorageDirectory().getPath() + "/vocinno/compression/";
//        String imgName= Long.toString(System.nanoTime())+".png";
        try {
            File hiddenDir = new File(savePath+".nomedia");
            if (!hiddenDir.exists()) {
                hiddenDir.mkdirs();
            }

            File f = new File(savePath,fileName);
            File file = new File(savePath);
            if(!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            return imgPath;
        }
        return savePath+fileName ;
    }


}
